package com.epfresh.views.dialog;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.bean.Comm;
import com.epfresh.bean.DeliveryModes;
import com.epfresh.bean.FilterCategory;
import com.epfresh.bean.GoodsParameters;
import com.epfresh.bean.Market;
import com.epfresh.bean.PaymentModes;
import com.epfresh.bean.ProductCategory;
import com.epfresh.bean.ProductCategoryNames;
import com.epfresh.bean.ProductCategoryVarieties;
import com.epfresh.bean.SaleTypes;
import com.epfresh.views.FilterTag;
import com.epfresh.views.tag.FlowLayout;
import com.epfresh.views.tag.TagAdapter;
import com.epfresh.views.tag.TagFlowLayout;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by wzh on 16-7-8.
 */
public class ProductFilterDialog extends BaseDialogView {


    OnFilterClickListener onFilterClickListener;

    //记录上次选中数据
    GoodsParameters parameters;

    String marketId = "";
    TextView tvReset;
    TextView tvSure;


    FilterTag ftCategory;
    FilterTag ftCategoryName;
    FilterTag ftCategoryVarieties;
    FilterTag ftMarket;
    FilterTag ftDistribution;
    FilterTag ftCategoryPayment;
    FilterTag ftCategorySale;

    FilterCategory filterCategory;

    TagAdapter<ProductCategory> adapterCategory;
    List<ProductCategory> categories = new ArrayList<>();

    TagAdapter<ProductCategoryNames> adapterCategoryName;
    List<ProductCategoryNames> categoryNames = new ArrayList<>();

    TagAdapter<ProductCategoryVarieties> adapterCategoryVarieties;
    List<ProductCategoryVarieties> categoryVarieties = new ArrayList<>();


    TagAdapter<Market> adapterMarket;
    List<Market> markets = new ArrayList<>();

    TagAdapter<DeliveryModes> adapterDelivery;
    List<DeliveryModes> delivery = new ArrayList<>();

    TagAdapter<PaymentModes> adapterPayment;
    List<PaymentModes> payment = new ArrayList<>();

    TagAdapter<SaleTypes> adapterSaleTypes;
    List<SaleTypes> saleTypes = new ArrayList<>();

    int tagW = -1;

    int TAG_MAX_COUNT = 6;

    //选中的分类下标
    int categorySelect = -1;
    //选中的品类下标
    int categoryNamesSelect = -1;
    //选中的品种集合
    Set<Integer> categoryVarietiesSet = new HashSet<>();
    //选中批发市场集合
    Set<Integer> marketSet = new HashSet<>();
    //选中配送方式集合
    Set<Integer> deliverySet = new HashSet<>();

    //选中付款方式集合
    Set<Integer> paymentSet = new HashSet<>();
    //选中销售方式集合
    Set<Integer> saleSet = new HashSet<>();


    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (view.getId() == R.id.tv_reset) {
                reset();
            } else if (view.getId() == R.id.tv_sure) {
                save();
            } else if (view.getId() == R.id.v_more) {
                Object tag = view.getTag(R.id.item_key_position);
                if ("categoryName".equals(tag)) {//点击品类全部
                    intoMoreName();
                } else if ("categoryVarieties".equals(tag)) {//点击品种全部
                    intoMoreVarieties();
                }
            }
        }
    };

    public GoodsParameters getParameters() {
        return parameters;
    }

    public void setParameters(GoodsParameters parameters) {
        this.parameters = parameters;
    }

    public String getMarketId() {
        return marketId;
    }

    public void setMarketId(String marketId) {
        this.marketId = marketId;
    }

    public OnFilterClickListener getOnFilterClickListener() {
        return onFilterClickListener;
    }

    public void setOnFilterClickListener(OnFilterClickListener onFilterClickListener) {
        this.onFilterClickListener = onFilterClickListener;
    }

    public ProductFilterDialog(Context context) {
        super(context);
        init();
    }

    @Override
    public void initBefore() {
        setGravity(Gravity.RIGHT);
    }

    public void init() {
        View v = initCategoryFilter();

        initCategoryMore(v);
    }

    public View initCategoryFilter() {


        View v = LayoutInflater.from(context).inflate(R.layout.dialog_category_filter, contentContainer);
        contentContainer.setBackgroundColor(Color.WHITE);
        ViewGroup.LayoutParams lp0 = contentContainer.getLayoutParams();

        ViewGroup.LayoutParams lp = v.getLayoutParams();

        lp.width = LocalDisplay.SCREEN_WIDTH_PIXELS * 4 / 5;
        lp0.width = lp.width;
        lp0.height = ViewGroup.LayoutParams.MATCH_PARENT;

        setCancelable(true);

        tagW = LocalDisplay.SCREEN_WIDTH_PIXELS * 4 / 5 - LocalDisplay.dp2px(4) * 8;
        tagW /= 3;

        tvReset = (TextView) findViewById(R.id.tv_reset);
        tvSure = (TextView) findViewById(R.id.tv_sure);

        tvReset.setOnClickListener(onClickListener);
        tvSure.setOnClickListener(onClickListener);

        ftCategory = (FilterTag) findViewById(R.id.ft_category);
        ftCategoryName = (FilterTag) findViewById(R.id.ft_category_name);
        ftCategoryVarieties = (FilterTag) findViewById(R.id.ft_category_varieties);
        ftMarket = (FilterTag) findViewById(R.id.ft_market);
        ftDistribution = (FilterTag) findViewById(R.id.ft_distribution);
        ftCategoryPayment = (FilterTag) findViewById(R.id.ft_payment);
        ftCategorySale = (FilterTag) findViewById(R.id.ft_sale);

        ftCategory.tvTitle.setText("分类");
        ftCategoryName.tvTitle.setText("品类");
        ftCategoryVarieties.tvTitle.setText("品种");
        ftMarket.tvTitle.setText("批发市场");
        ftDistribution.tvTitle.setText("配送方式");
        ftCategoryPayment.tvTitle.setText("付款方式");
        ftCategorySale.tvTitle.setText("销售方式");

        ftCategory.tagView.setMaxSelectCount(1);
        ftCategoryName.tagView.setMaxSelectCount(1);


        if (adapterCategory == null)
            adapterCategory = new TagAdapter<ProductCategory>(categories) {
                @Override
                public View getView(FlowLayout parent, int position, ProductCategory s) {
                    TextView tv = (TextView) LayoutInflater.from(context).inflate(R.layout.tag,
                            parent, false);
                    ViewGroup.LayoutParams lp = tv.getLayoutParams();
                    lp.width = tagW;
                    tv.setText(s.getName());
                    return tv;
                }
            };

        ftCategory.tagView.setAdapter(adapterCategory);


        if (adapterCategoryName == null)
            adapterCategoryName = new TagAdapter<ProductCategoryNames>(categoryNames) {
                @Override
                public View getView(FlowLayout parent, int position, ProductCategoryNames s) {
                    if (position + 1 == TAG_MAX_COUNT && categoryNames.size() > TAG_MAX_COUNT) {
                        View v = LayoutInflater.from(context).inflate(R.layout.tag_more,
                                parent, false);
                        TextView tv = (TextView) v.findViewById(R.id.tv_title);
                        ViewGroup.LayoutParams lp = v.getLayoutParams();
                        lp.width = tagW;
                        tv.setText(context.getString(R.string.all, "品类"));
                        v.setTag(R.id.item_key_position, "categoryName");
                        v.setOnClickListener(onClickListener);
                        return v;
                    }
                    TextView tv = (TextView) LayoutInflater.from(context).inflate(R.layout.tag,
                            parent, false);
                    ViewGroup.LayoutParams lp = tv.getLayoutParams();
                    lp.width = tagW;
                    tv.setText(s.getName());
                    return tv;
                }

                @Override
                public int getCount() {
                    int count = categoryNames.size();
                    if (count > TAG_MAX_COUNT) {
                        count = TAG_MAX_COUNT;
                    }
                    return count;
                }
            };

        ftCategoryName.tagView.setAdapter(adapterCategoryName);

        if (adapterCategoryVarieties == null)
            adapterCategoryVarieties = new TagAdapter<ProductCategoryVarieties>(categoryVarieties) {
                @Override
                public View getView(FlowLayout parent, int position, ProductCategoryVarieties s) {
                    if (position + 1 == TAG_MAX_COUNT && categoryVarieties.size() > TAG_MAX_COUNT) {
                        View v = LayoutInflater.from(context).inflate(R.layout.tag_more,
                                parent, false);
                        TextView tv = (TextView) v.findViewById(R.id.tv_title);
                        ViewGroup.LayoutParams lp = v.getLayoutParams();
                        lp.width = tagW;
                        tv.setText(context.getString(R.string.all, "品种"));
                        v.setTag(R.id.item_key_position, "categoryVarieties");
                        v.setOnClickListener(onClickListener);
                        return v;
                    }
                    TextView tv = (TextView) LayoutInflater.from(context).inflate(R.layout.tag,
                            parent, false);
                    ViewGroup.LayoutParams lp = tv.getLayoutParams();
                    lp.width = tagW;
                    tv.setText(s.getName());
                    return tv;
                }

                @Override
                public int getCount() {
                    int count = categoryVarieties.size();
                    if (count > TAG_MAX_COUNT) {
                        count = TAG_MAX_COUNT;
                    }
                    return count;
                }
            };

        ftCategoryVarieties.tagView.setAdapter(adapterCategoryVarieties);

        if (adapterMarket == null) {
            adapterMarket = new TagAdapter<Market>(markets) {
                @Override
                public View getView(FlowLayout parent, int position, Market s) {

                    TextView tv = (TextView) LayoutInflater.from(context).inflate(R.layout.tag,
                            parent, false);
                    ViewGroup.LayoutParams lp = tv.getLayoutParams();
                    lp.width = tagW;
                    tv.setText(s.getShortName());
                    return tv;
                }

            };
        }

        ftMarket.tagView.setAdapter(adapterMarket);


        if (adapterDelivery == null) {
            adapterDelivery = new TagAdapter<DeliveryModes>(delivery) {
                @Override
                public View getView(FlowLayout parent, int position, DeliveryModes s) {
                    TextView tv = (TextView) LayoutInflater.from(context).inflate(R.layout.tag,
                            parent, false);
                    ViewGroup.LayoutParams lp = tv.getLayoutParams();
                    lp.width = tagW;
                    tv.setText(s.getName());
                    return tv;
                }
            };
        }

        ftDistribution.tagView.setAdapter(adapterDelivery);

        if (adapterPayment == null) {
            adapterPayment = new TagAdapter<PaymentModes>(payment) {
                @Override
                public View getView(FlowLayout parent, int position, PaymentModes s) {
                    TextView tv = (TextView) LayoutInflater.from(context).inflate(R.layout.tag,
                            parent, false);
                    ViewGroup.LayoutParams lp = tv.getLayoutParams();
                    lp.width = tagW;
                    tv.setText(s.getName());
                    return tv;
                }
            };
        }

        ftCategoryPayment.tagView.setAdapter(adapterPayment);


        if (adapterSaleTypes == null) {
            adapterSaleTypes = new TagAdapter<SaleTypes>(saleTypes) {
                @Override
                public View getView(FlowLayout parent, int position, SaleTypes s) {
                    TextView tv = (TextView) LayoutInflater.from(context).inflate(R.layout.tag,
                            parent, false);
                    ViewGroup.LayoutParams lp = tv.getLayoutParams();
                    lp.width = tagW;
                    tv.setText(s.getName());
                    return tv;
                }
            };
        }

        ftCategorySale.tagView.setAdapter(adapterSaleTypes);


        ftCategory.tagView.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {
                doSelectCategory(selectPosSet);
            }
        });
        ftCategoryName.tagView.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {

                doSelectCategoryNames(selectPosSet);
            }
        });
        ftCategoryVarieties.tagView.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {
                doSelectCategoryVarieties(selectPosSet);
            }
        });

        ftMarket.tagView.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {
                doSelectMarket(selectPosSet);
            }
        });
        ftDistribution.tagView.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {
                doSelectDistribution(selectPosSet);
            }
        });

        ftCategoryPayment.tagView.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {
                doSelectPayment(selectPosSet);
            }
        });
        ftCategorySale.tagView.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {
                doSelectSale(selectPosSet);
            }
        });

        return v;
    }

    private void doSelectSale(Set<Integer> selectPosSet) {
        StringBuilder sb = new StringBuilder();
        int count = saleTypes.size();
        saleSet.clear();
        saleSet.addAll(selectPosSet);
        for (Integer i : selectPosSet) {
            if (i < count) {
                sb.append(saleTypes.get(i).getName());
                sb.append(",");
            }
        }
        String s = sb.toString();
        if (sb.length() > 0 && sb.lastIndexOf(",") == sb.length() - 1) {
            s = sb.subSequence(0, sb.length() - 1).toString();
        }
        ftCategorySale.tvContent.setText(s);
    }

    private void doSelectPayment(Set<Integer> selectPosSet) {
        StringBuilder sb = new StringBuilder();
        int count = payment.size();
        paymentSet.clear();
        paymentSet.addAll(selectPosSet);
        for (Integer i : selectPosSet) {
            if (i < count) {
                sb.append(payment.get(i).getName());
                sb.append(",");
            }
        }
        String s = sb.toString();
        if (sb.length() > 0 && sb.lastIndexOf(",") == sb.length() - 1) {
            s = sb.subSequence(0, sb.length() - 1).toString();
        }
        ftCategoryPayment.tvContent.setText(s);
    }

    private void doSelectDistribution(Set<Integer> selectPosSet) {
        StringBuilder sb = new StringBuilder();
        int count = delivery.size();
        deliverySet.clear();
        deliverySet.addAll(selectPosSet);
        for (Integer i : selectPosSet) {
            if (i < count) {
                sb.append(delivery.get(i).getName());
                sb.append(",");
            }
        }
        String s = sb.toString();
        if (sb.length() > 0 && sb.lastIndexOf(",") == sb.length() - 1) {
            s = sb.subSequence(0, sb.length() - 1).toString();
        }
        ftDistribution.tvContent.setText(s);
    }

    private void doSelectMarket(Set<Integer> selectPosSet) {
        StringBuilder sb = new StringBuilder();
        int count = markets.size();
        marketSet.clear();
        marketSet.addAll(selectPosSet);
        for (Integer i : selectPosSet) {
            if (i < count) {
                sb.append(markets.get(i).getShortName());
                sb.append(",");
            }
        }
        String s = sb.toString();
        if (sb.length() > 0 && sb.lastIndexOf(",") == sb.length() - 1) {
            s = sb.subSequence(0, sb.length() - 1).toString();
        }
        ftMarket.tvContent.setText(s);
    }

    private void doSelectCategoryVarieties(Set<Integer> selectPosSet) {
        StringBuilder sb = new StringBuilder();
        int count = categoryVarieties.size();
        categoryVarietiesSet.clear();
        categoryVarietiesSet.addAll(selectPosSet);
        for (Integer i : selectPosSet) {
            if (i < count) {
                sb.append(categoryVarieties.get(i).getName());
                sb.append(",");
            }
        }
        String s = sb.toString();
        if (sb.length() > 0 && sb.lastIndexOf(",") == sb.length() - 1) {
            s = sb.subSequence(0, sb.length() - 1).toString();
        }
        ftCategoryVarieties.tvContent.setText(s);
    }

    private void doSelectCategoryNames(Set<Integer> selectPosSet) {
        StringBuilder sb = new StringBuilder();
        int count = categoryNames.size();
        int position = -1;
        ftCategoryVarieties.tvContent.setText("");
        categoryNamesSelect = -1;
        categoryVarietiesSet.clear();
        adapterCategoryVarieties.setSelectedList(categoryVarietiesSet);
        for (Integer i : selectPosSet) {
            if (i < count) {
                position = i;
                categoryNamesSelect = i;
                sb.append(categoryNames.get(i).getName());
                break;
            }
        }

        ftCategoryName.tvContent.setText(sb.toString());

        if (selectPosSet.size() > 0) {//加载品种

            if (position > -1 && position < categoryNames.size()) {
                ProductCategoryNames productCategory = categoryNames.get(position);
                List<ProductCategoryVarieties> names = productCategory.getVarieties();
                if (names != null && names.size() > 0) {
                    ftCategoryVarieties.setVisibility(View.VISIBLE);
                    updateVarieties(names);
                    return;
                }

            }
            ftCategoryVarieties.setVisibility(View.GONE);
        } else {//删除品类
            ftCategoryVarieties.setVisibility(View.GONE);
        }
        ftCategoryVarieties.tvContent.setText("");
    }

    private void doSelectCategory(Set<Integer> selectPosSet) {
        StringBuilder sb = new StringBuilder();
        int count = categories.size();
        int position = -1;
        ftCategoryName.tvContent.setText("");
        ftCategoryVarieties.tvContent.setText("");
        categorySelect = -1;
        categoryNamesSelect = -1;
        categoryVarietiesSet.clear();
        adapterCategoryName.setSelectedList(new HashSet<Integer>());
        adapterCategoryVarieties.setSelectedList(categoryVarietiesSet);
        for (Integer i : selectPosSet) {
            if (i < count) {
                position = i;
                categorySelect = i;
                sb.append(categories.get(i).getName());
                break;
            }
        }
        ftCategory.tvContent.setText(sb.toString());
        if (selectPosSet.size() > 0) {//加载品类
            ftCategoryVarieties.setVisibility(View.GONE);
            if (position > -1 && position < categories.size()) {
                ProductCategory productCategory = categories.get(position);
                List<ProductCategoryNames> names = productCategory.getItem();
                if (names != null && names.size() > 0) {
                    ftCategoryName.setVisibility(View.VISIBLE);
                    updateName(names);
                    return;
                }

            }
            ftCategoryName.setVisibility(View.GONE);
        } else {//删除品类
            ftCategoryName.setVisibility(View.GONE);
            ftCategoryVarieties.setVisibility(View.GONE);
        }
    }

    public void updateName(List<ProductCategoryNames> names) {

        this.categoryNames.clear();
        this.categoryNames.addAll(names);
        adapterCategoryName.notifyDataChanged();

    }

    public void updateVarieties(List<ProductCategoryVarieties> varieties) {
        this.categoryVarieties.clear();
        this.categoryVarieties.addAll(varieties);
        adapterCategoryVarieties.notifyDataChanged();
    }


    public void updateCategory(FilterCategory filterCategory) {
        if (marketId != null && !"".equals(marketId)) {
            ftMarket.setVisibility(View.GONE);
        } else {
            ftMarket.setVisibility(View.VISIBLE);

        }
        clearStatus();
        rlCategoryMore.setVisibility(View.GONE);
        isMoreShowing = false;
        isMoreDismiss = true;
        ftCategoryName.setVisibility(View.GONE);
        ftCategoryVarieties.setVisibility(View.GONE);


        this.filterCategory = filterCategory;
        List<ProductCategory> categories = filterCategory.getCategories();

        this.categories.clear();
        this.categories.addAll(categories);
        adapterCategory.notifyDataChanged();

        adapterCategoryName.notifyDataChanged();
        adapterCategoryVarieties.notifyDataChanged();

        //测试
        List<Market> markets = filterCategory.getMarkets();

        this.markets.clear();
        this.markets.addAll(markets);
        adapterMarket.notifyDataChanged();


        List<DeliveryModes> deliveryModes = filterCategory.getDeliveryModes();

        this.delivery.clear();
        if (deliveryModes != null){
            this.delivery.addAll(deliveryModes);
        }else {
            ftDistribution.setVisibility(View.GONE);
        }
        adapterDelivery.notifyDataChanged();


        List<PaymentModes> paymentModes = filterCategory.getPaymentModes();
        this.payment.clear();
        this.payment.addAll(paymentModes);
        adapterPayment.notifyDataChanged();


        List<SaleTypes> saleTypes = filterCategory.getSaleTypes();
        this.saleTypes.clear();
        this.saleTypes.addAll(saleTypes);
        adapterSaleTypes.notifyDataChanged();


    }

    private void save() {
        if (onFilterClickListener == null) {
            dismiss();
            return;
        }
        GoodsParameters parameters = new GoodsParameters();

        int count = 0;
        //分类级联筛选条件装载
        if (categorySelect > -1 && categorySelect < categories.size()) {

            ProductCategory p = categories.get(categorySelect);
            count++;
            //添加分类选择id
            parameters.setCategoryId(p.getId());
            List<ProductCategoryNames> names = p.getItem();
            if (categoryNamesSelect > -1 && names != null && names.size() > categoryNamesSelect) {

                count++;
                ProductCategoryNames name = names.get(categoryNamesSelect);
                //添加品类id
                parameters.setNameId(name.getId());

                List<ProductCategoryVarieties> varieties = name.getVarieties();
                if (varieties != null && categoryVarietiesSet != null && categoryVarietiesSet.size() > 0) {
                    List<String> varietiyIds = new ArrayList<>();
                    for (Integer i : categoryVarietiesSet) {
                        if (i != null && i > -1 && i < varieties.size()) {
                            ProductCategoryVarieties variety = varieties.get(i);
                            varietiyIds.add(variety.getId());
                        }
                    }
                    if (varietiyIds.size() > 0) {
                        count++;
                        parameters.setVarietyIds(varietiyIds);
                    }
                }

            }
        }

        if (marketId != null && !"".equals(marketId)) {
            List<String> marketIds = new ArrayList<>();
            marketIds.add(marketId);
            parameters.setMarketIds(marketIds);
        }
        //市场筛选条件装载
        else if (markets != null && markets.size() > 0) {
            List<String> marketIds = new ArrayList<>();
            for (Integer i : marketSet) {
                if (i != null && i > -1 && i < markets.size()) {
                    Market market = markets.get(i);
                    marketIds.add(market.getId() + "");
                }
            }
            if (marketIds.size() > 0) {
                count++;
                parameters.setMarketIds(marketIds);
            }
        }

        //配送方式筛选条件装载
        if (delivery != null && delivery.size() > 0) {
            List<String> ids = new ArrayList<>();
            for (Integer i : deliverySet) {
                if (i != null && i > -1 && i < delivery.size()) {
                    DeliveryModes m = delivery.get(i);
                    ids.add(m.getCode());
                }
            }
            if (ids.size() > 0) {
                count++;
                parameters.setDeliveryModes(ids);
            }
        }
        //付款方式筛选条件装载
        if (payment != null && paymentSet.size() > 0) {
            List<String> ids = new ArrayList<>();
            for (Integer i : paymentSet) {
                if (i != null && i > -1 && i < payment.size()) {
                    PaymentModes m = payment.get(i);
                    ids.add(m.getCode() + "");
                }
            }
            if (ids.size() > 0) {
                count++;
                parameters.setPaymentModes(ids);
            }
        }
        //销售方式筛选条件装载
        if (saleTypes != null && saleSet.size() > 0) {
            List<String> ids = new ArrayList<>();
            for (Integer i : saleSet) {
                if (i != null && i > -1 && i < saleTypes.size()) {
                    SaleTypes m = saleTypes.get(i);
                    ids.add(m.getType() + "");
                }
            }
            if (ids.size() > 0) {
                count++;
                parameters.setTypes(ids);
            }
        }

        if (count > 0) {

        } else {
            parameters = null;
        }

        onFilterClickListener.onFilter(parameters);
        onFilterClickListener = null;
        dismiss();
    }

    private void reset() {

        categorySelect = -1;
        ftCategory.tvContent.setText("");
        adapterCategory.setSelectedList(categorySelect);

        categoryNames.clear();
        categoryNamesSelect = -1;
        ftCategoryName.setVisibility(View.GONE);
        ftCategoryName.tvContent.setText("");

        categoryVarieties.clear();
        categoryVarietiesSet.clear();
        ftCategoryVarieties.setVisibility(View.GONE);
        ftCategoryVarieties.tvContent.setText("");


        marketSet.clear();
        ftMarket.tvContent.setText("");
        adapterMarket.setSelectedList(marketSet);

        deliverySet.clear();
        ftDistribution.tvContent.setText("");
        adapterDelivery.setSelectedList(deliverySet);

        paymentSet.clear();
        ftCategoryPayment.tvContent.setText("");
        adapterPayment.setSelectedList(paymentSet);

        saleSet.clear();
        ftCategorySale.tvContent.setText("");
        adapterSaleTypes.setSelectedList(saleSet);

    }


    public void clearStatus() {
        categories.clear();
        categorySelect = -1;
        ftCategory.tvContent.setText("");
        adapterCategory.setSelectedList(categorySelect);

        categoryNames.clear();
        categoryNamesSelect = -1;
        ftCategoryName.tvContent.setText("");
        adapterCategoryName.setSelectedList(categoryNamesSelect);

        categoryVarieties.clear();
        categoryVarietiesSet.clear();
        ftCategoryVarieties.tvContent.setText("");
        adapterCategoryVarieties.setSelectedList(categoryVarietiesSet);

        markets.clear();
        marketSet.clear();
        ftMarket.tvContent.setText("");
        adapterMarket.setSelectedList(marketSet);

        delivery.clear();
        deliverySet.clear();
        ftDistribution.tvContent.setText("");
        adapterDelivery.setSelectedList(deliverySet);

        payment.clear();
        paymentSet.clear();
        ftCategoryPayment.tvContent.setText("");
        adapterPayment.setSelectedList(paymentSet);

        saleTypes.clear();
        saleSet.clear();
        ftCategorySale.tvContent.setText("");
        adapterSaleTypes.setSelectedList(saleSet);

    }


    public void initCategory(GoodsParameters parameters, FilterCategory filterCategory) {
        String categoryId = parameters.getCategoryId();
        //分类级联初始化
        if (categoryId != null && !"".equals(categoryId)) {
            List<ProductCategory> categories = filterCategory.getCategories();
            if (categories != null) {
                for (int i = 0; i < categories.size(); i++) {
                    ProductCategory p = categories.get(i);
                    if (categoryId.equals(p.getId())) {//当前选中的
                        categorySelect = i;//分类选中的当前位置
                        adapterCategory.setSelectedList(i);
                        doSelectCategory(ftCategory.tagView.getSelectedList());
                        List<ProductCategoryNames> names = p.getItem();
                        String nameId = parameters.getNameId();
                        if (nameId != null && !"".equals(nameId) && names != null) {
                            for (int j = 0; j < names.size(); j++) {
                                ProductCategoryNames pn = names.get(j);
                                if (nameId.equals(pn.getId())) {
                                    categoryNamesSelect = j;//品类选选中的当前位置
                                    categoryNames.clear();
                                    categoryNames.addAll(names);
                                    adapterCategoryName.setSelectedList(j);
                                    ftCategoryName.setVisibility(View.VISIBLE);
                                    doSelectCategoryNames(ftCategoryName.tagView.getSelectedList());
                                    List<ProductCategoryVarieties> varieties = pn.getVarieties();

                                    List<String> varietyIds = parameters.getVarietyIds();


                                    if (varieties != null && varietyIds != null && varietyIds.size() > 0) {
                                        for (int k = 0; k < varieties.size(); k++) {
                                            ProductCategoryVarieties pv = varieties.get(k);

                                            for (int m = 0; m < varietyIds.size(); m++) {
                                                String s = varietyIds.get(m);

                                                if (s != null && !"".equals(s) && s.equals(pv.getId())) {
                                                    categoryVarietiesSet.add(k);
                                                }
                                            }
                                        }
                                        if (categoryVarietiesSet.size() >0) {
                                            categoryVarieties.clear();
                                            categoryVarieties.addAll(varieties);
                                            adapterCategoryVarieties.setSelectedList(categoryVarietiesSet);
                                            ftCategoryVarieties.setVisibility(View.VISIBLE);
                                            doSelectCategoryVarieties(ftCategoryVarieties.tagView.getSelectedList());
                                        }

                                    }

                                    return;
                                }
                            }
                        }
                        return;
                    }
                }

            }
        }


    }

    public void initMarket(GoodsParameters parameters, FilterCategory filterCategory) {
        //市场初始化
        List<String> marketIds = parameters.getMarketIds();

        List<Market> markets = filterCategory.getMarkets();

        if (marketIds != null && marketIds.size() > 0 && markets != null) {
            for (int i = 0; i < markets.size(); i++) {

                Market m = markets.get(i);

                for (int j = 0; j < marketIds.size(); j++) {
                    String s = marketIds.get(j);
                    if (s != null && !"".equals(s) && s.equals(m.getId())) {
                        marketSet.add(i);
                    }
                }
            }
            adapterMarket.setSelectedList(marketSet);
            doSelectMarket(ftMarket.tagView.getSelectedList());
        }

    }

    public void initDelivery(GoodsParameters parameters, FilterCategory filterCategory) {
        //配送方式初始化
        List<String> deliveryIds = parameters.getDeliveryModes();

        List<DeliveryModes> deliveryModes = filterCategory.getDeliveryModes();

        if (deliveryIds != null && deliveryIds.size() > 0 && deliveryModes != null) {
            for (int i = 0; i < deliveryModes.size(); i++) {

                DeliveryModes m = deliveryModes.get(i);

                for (int j = 0; j < deliveryIds.size(); j++) {
                    String s = deliveryIds.get(j);
                    if (s != null && !"".equals(s) && s.equals(m.getCode())) {
                        deliverySet.add(i);
                    }
                }
            }
            adapterDelivery.setSelectedList(deliverySet);
            doSelectDistribution(ftDistribution.tagView.getSelectedList());
        }

    }

    public void initPayment(GoodsParameters parameters, FilterCategory filterCategory) {
        //支付方式初始化
        List<String> ids = parameters.getPaymentModes();

        List<PaymentModes> deliveryModes = filterCategory.getPaymentModes();

        if (ids != null && ids.size() > 0 && deliveryModes != null) {
            for (int i = 0; i < deliveryModes.size(); i++) {

                PaymentModes m = deliveryModes.get(i);

                for (int j = 0; j < ids.size(); j++) {
                    String s = ids.get(j);
                    if (s != null && !"".equals(s) && s.equals(m.getCode())) {
                        paymentSet.add(i);
                    }
                }
            }
            adapterPayment.setSelectedList(paymentSet);
            doSelectPayment(ftCategoryPayment.tagView.getSelectedList());
        }

    }

    public void initSale(GoodsParameters parameters, FilterCategory filterCategory) {
        //支付方式初始化
        List<String> ids = parameters.getTypes();

        List<SaleTypes> deliveryModes = filterCategory.getSaleTypes();

        if (ids != null && ids.size() > 0 && deliveryModes != null) {
            for (int i = 0; i < deliveryModes.size(); i++) {

                SaleTypes m = deliveryModes.get(i);

                for (int j = 0; j < ids.size(); j++) {
                    String s = ids.get(j);
                    if (s != null && !"".equals(s) && s.equals(m.getType())) {
                        saleSet.add(i);
                    }
                }
            }
            adapterSaleTypes.setSelectedList(saleSet);
            doSelectSale(ftCategorySale.tagView.getSelectedList());
        }

    }

    public void initStatus(FilterCategory filterCategory) {

        updateCategory(filterCategory);
        if (parameters != null && filterCategory != null) {
            initCategory(parameters, filterCategory);

            if (marketId == null || "".equals(marketId))
                initMarket(parameters, filterCategory);

            initDelivery(parameters, filterCategory);

            initPayment(parameters, filterCategory);

            initSale(parameters, filterCategory);

        }


    }


    private void intoMoreVarieties() {
        showCategoryMore(categoryVarieties, categoryVarietiesSet, -1);
    }

    private void intoMoreName() {
        Set<Integer> set = null;
        if (categoryNamesSelect > -1) {
            set = new HashSet<>();
            set.add(categoryNamesSelect);
        }
        showCategoryMore(categoryNames, set, 1);
    }


    View rlCategoryMore;
    View vBack;
    TextView tvTitle;
    TextView tvRight;

    Animation inAnim2;
    Animation outAnim2;

    boolean isMoreShowing;
    boolean isMoreDismiss = true;

    TagFlowLayout ftCategoryMore;
    TagAdapter<Comm> adapterCategoryMore;
    List<Comm> categoryMore = new ArrayList<>();

    private View.OnClickListener onClickMoreListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (view.getId() == R.id.baseback) {
                dismissCategoryMore();
            } else if (view.getId() == R.id.rightbtn) {

                Object tag = view.getTag(R.id.item_key_position);
                if ("categoryName".equals(tag)) {//点击品类全部
                    saveCategoryNameMore();
                } else if ("categoryVarieties".equals(tag)) {//点击品种全部
                    saveCategoryVarietiesMore();
                }
                dismissCategoryMore();
            }

        }
    };


    public void initCategoryMore(View view) {
        rlCategoryMore = view.findViewById(R.id.v_category_all);
        rlCategoryMore.setVisibility(View.GONE);
        tvTitle = (TextView) rlCategoryMore.findViewById(com.epfresh.api.R.id.toolbar_title);
        vBack = rlCategoryMore.findViewById(com.epfresh.api.R.id.baseback);
        tvRight = (TextView) rlCategoryMore.findViewById(com.epfresh.api.R.id.rightbtn);

        ftCategoryMore = (TagFlowLayout) findViewById(R.id.t_tag_more);


        vBack.setVisibility(View.VISIBLE);
        tvRight.setText(R.string.sure);
        tvTitle.setVisibility(View.VISIBLE);
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setOnClickListener(onClickMoreListener);

        vBack.setOnClickListener(onClickMoreListener);

        inAnim2 = AnimationUtils.loadAnimation(context, R.anim.anim_right_push_in);
        outAnim2 = AnimationUtils.loadAnimation(context, R.anim.anim_right_push_out);

        rlCategoryMore.setVisibility(View.GONE);
        inAnim2.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                isMoreShowing = true;
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                isMoreDismiss = false;

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        outAnim2.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                isMoreDismiss = true;
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                isMoreShowing = false;

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        if (adapterCategoryMore == null) {
            adapterCategoryMore = new TagAdapter<Comm>(categoryMore) {
                @Override
                public View getView(FlowLayout parent, int position, Comm s) {
                    TextView tv = (TextView) LayoutInflater.from(context).inflate(R.layout.tag,
                            parent, false);
                    ViewGroup.LayoutParams lp = tv.getLayoutParams();
                    lp.width = tagW * 3 / 2;
//                    tv.setMinWidth(tagW *3/2);
                    tv.setText(s.getName());
                    return tv;
                }
            };
        }
        ftCategoryMore.setAdapter(adapterCategoryMore);
    }

    public void dismissCategoryMore() {
        if (!isMoreDismiss) {
            rlCategoryMore.startAnimation(outAnim2);
            rlCategoryMore.setVisibility(View.GONE);
        }
    }

    public boolean isCategoryMoreShowing() {
        if (rlCategoryMore != null && rlCategoryMore.getVisibility() == View.VISIBLE) {
            return true;
        }
        return false;
    }

    //全部品种点击确定
    private void saveCategoryVarietiesMore() {
        Set<Integer> set = ftCategoryMore.getSelectedList();
        adapterCategoryVarieties.setSelectedList(set);

        StringBuilder sb = new StringBuilder();
        int count = categoryVarieties.size();
        categoryVarietiesSet.addAll(set);
        for (Integer i : set) {
            if (i < count) {
                sb.append(categoryVarieties.get(i).getName());
                sb.append(",");
            }
        }
        String s = sb.toString();
        if (sb.length() > 0 && sb.lastIndexOf(",") == sb.length() - 1) {
            s = sb.subSequence(0, sb.length() - 1).toString();
        }
        ftCategoryVarieties.tvContent.setText(s);

    }

    //全部品类点击确定
    private void saveCategoryNameMore() {
        Set<Integer> set = ftCategoryMore.getSelectedList();
        adapterCategoryName.setSelectedList(set);

        StringBuilder sb = new StringBuilder();
        int count = categoryNames.size();
        ftCategoryVarieties.tvContent.setText("");
        categoryVarietiesSet.clear();
        int position = -1;
        for (Integer i : set) {
            if (i < count) {
                categoryNamesSelect = i;
                position = i;
                sb.append(categoryNames.get(i).getName());
                break;
            }
        }

        ftCategoryName.tvContent.setText(sb.toString());


        if (set.size() > 0) {//加载品种

            if (position > 0 && position < categoryNames.size()) {
                ProductCategoryNames productCategory = categoryNames.get(position);
                List<ProductCategoryVarieties> names = productCategory.getVarieties();
                if (names != null && names.size() > 0) {
                    ftCategoryVarieties.setVisibility(View.VISIBLE);
                    updateVarieties(names);
                    return;
                }

            }
            ftCategoryVarieties.setVisibility(View.GONE);
        } else {//删除品类
            ftCategoryVarieties.setVisibility(View.GONE);
        }
        ftCategoryVarieties.tvContent.setText("");
    }


    public void showCategoryMore(List<? extends Comm> comm, Set<Integer> set, int type) {

        if (!isMoreShowing) {
            rlCategoryMore.startAnimation(inAnim2);
            rlCategoryMore.setVisibility(View.VISIBLE);
            //type == -1,表示全部品种,多选,type == 1,表示品类,单选
            ftCategoryMore.setMaxSelectCount(type);
            if (type < 0) {
                tvTitle.setText("品种");
                tvRight.setTag(R.id.item_key_position, "categoryVarieties");
            } else {
                tvTitle.setText("品类");
                tvRight.setTag(R.id.item_key_position, "categoryName");
            }

            categoryMore.clear();
            if (comm != null)
                categoryMore.addAll(comm);
            if (set != null)
                adapterCategoryMore.setSelectedList(set);


            adapterCategoryMore.notifyDataChanged();
        }
    }


}
